<?php


namespace app\api\controller;
use app\common\lib\db\DbCommon;
use app\common\lib\exception\ApiException;
use think\Db;

/**
 * 处理配置相关参数
 * Class Settings
 * @package app\api\controller
 */
class Settings extends Base
{
    /**
     * 不限制请求方式
     * @return string $data 关于我们
     */
    public function about_we()
    {
        try{
            $data = $this->get_we_Msg('we_id','1');
        }catch (\Exception $e){
            throw new ApiException($e->getMessage(),'500');
        }
        if(!$data) {
            throw new ApiException('当前请求无效','500');
        }
        return show('200','ok',$data);
    }

    /**
     * @param number $we_id 指定"关于我们"主键记录
     * @param number $id 指定第几条
     * @return array $data
     */
    public function get_we_Msg($we_id,$id)
    {
        return $this->_we->get_type_msg($we_id,$id);
    }

    /**
     * 无请求要求
     * @return array $data
     */
    public function tips_msg()
    {
        if(request()->isPOST()) {
            $data = input('post.');
            if(!isset($data['nowPage']) || !isset($data['limits']) || empty($data['nowPage']) || empty($data['limits'])) {
                throw new ApiException('请传入页码信息','500');
            }
//            halt($data);
            $count_page = $this->_notice->count_all_num();
            if($data['limits'] > $count_page && $data['nowPage'] > 1) {
                throw new ApiException('没有更多了','200');
            }
            $data = $this->get_only_tips('notice_id', 'desc',$data['nowPage'],$data['limits']);
            if (!$data) {
                return show('200', '当前无系统通知');
            }
            return show('200', 'ok', $data);
        }
        throw new ApiException('非法请求','500');
    }

    /**
     * @param string $key 指定健
     * @param string $order 指定排序规则
     * @return bool
     */
    public function get_only_tips($key,$order,$page=1,$limits=10)
    {
        try{
            $data = $this->_notice->get_order_msg($key,$order,$page,$limits);
        }catch (\Exception $e){
            throw new ApiException($e->getMessage(),'500');
        }
        if(!count($data)) {
            return false;
        }
        foreach ($data as $key => $value) {
            unset($value['notice_id'],$value['notice_user'],$value['notice_read_user'],$value['notice_del_user'],$value['notice_send_user'],$value['notice_update_time']);
        }
        return $data;
    }

    /**
     * post / get 无特定请求协议
     * @return array $data 所有剧团剧院信息
     */
    public function troupe_list()
    {
        try{
            $data = DbCommon::get_troupe_list();
        }catch (\Exception $e){
            throw new ApiException($e->getMessage(),'500');
        }
        return show('200','ok',$data);
    }

    public function troupe_con()
    {
        if(\request()->isPOST()) {
            $data = input('post.');
            $is_array = check_array($data); //基本判断请求是否合法
            if(!$is_array  || !isset($data['troupe_id']) || empty($data['troupe_id'])) {
                throw new ApiException('数据请求不合法','500');
            }
            try{
                $ret = $this->_troupe->get(['troupe_id'=>$data['troupe_id']]);
            }catch (\Exception $e) {
                throw new ApiException($e->getMessage());
            }
            if(!$ret) {
                throw new ApiException('数据库错误','500');
            }
            return show('200','ok',[
                'name'   => $ret['troupe_name'],
                'time'   => $ret['troupe_time'],
                'content'=> $ret['troupe_content'],
            ]);
        }
        throw new ApiException('非法请求','500');
    }
}